﻿using Liuliu.ScriptEngine;
using Liuliu.ScriptEngine.Damo;
using OSharp.Utility.Data;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Net;

namespace GmControlTools
{
    public class TaskHandler
    {
        public readonly static TaskHandler Instance = new TaskHandler();
        private ConcurrentQueue<TaskModle> ListQueue = new ConcurrentQueue<TaskModle>();

        /// <summary>
        /// 加入任务队列
        /// </summary>
        public void AddQueue(TaskModle taskInfo)
        {
            Console.WriteLine($"====加入公告队列：{taskInfo.NameOrContent}");
            ListQueue.Enqueue(taskInfo);
        }

        /// <summary>
        /// 启动任务
        /// </summary>
        public void Start()
        {
            Task.Run(() => { ThreadStartAsync(); });
        }

        private void ThreadStartAsync()
        {
            while (true)
            {
                try
                {
                    if (ListQueue.TryDequeue(result: out TaskModle? item))
                    {
                        ScanQueue(item);
                    }
                }
                catch (Exception ex)
                {
                    // 异常处理
                    Console.WriteLine("任务队列出现异常:" + ex.Message);
                    continue;
                }
                // 等待任务执行完成
                Thread.Sleep(1000);
            }

        }

        /// <summary>
        /// 执行任务
        /// </summary>
        private bool ScanQueue(TaskModle queueinfo)
        {
            try
            {
                if (queueinfo != null)
                {
                    Console.WriteLine($"====取出公告队列：{queueinfo.NameOrContent}");
                    bool IsSuccess = false;
                    switch (queueinfo.actionType)
                    {
                        case 1://下线玩家
                            IsSuccess = OfflineUser.OfflineUserByName(queueinfo.NameOrContent);
                            break;
                        case 2://发布公告
                            IsSuccess = SendNotice.SendNoticeMessage(queueinfo.NameOrContent, 1, queueinfo.NoticeDate);
                            break;
                        default:
                            break;
                    }
                    return IsSuccess;
                }
            }
            catch (TimeoutException e)
            {
                Console.WriteLine($"超时报错：{e.Message}");
                return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine("执行任务出现异常:" + ex.Message);
                return false;
            }
            return true;
        }

        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="actionType">任务类型:1踢人2发公告</param>
        /// <param name="NameOrContent">下线用户名或公告内容</param>
        /// <param name="NoticeType">公告类型：1立即发送2定时发送</param>
        /// <param name="NoticeDate">定时发送时间</param>
        public static bool StartTask(int actionType, string NameOrContent, int NoticeType, string NoticeDate)
        {
            bool IsSuccess = false;
            switch (actionType)
            {
                case 1://下线玩家
                    IsSuccess = OfflineUser.OfflineUserByName(NameOrContent);
                    break;
                case 2://发布公告
                    IsSuccess = SendNotice.SendNoticeMessage(NameOrContent, 1, NoticeDate);
                    break;
                default:
                    break;
            }
            return IsSuccess;
        }

    }

    /// <summary>
    /// 任务
    /// </summary>
    public class TaskModle
    {
        /// <summary>
        /// 任务类型:1踢人2发公告
        /// </summary>
        public int actionType { get; set; }
        /// <summary>
        /// 下线用户名或公告内容
        /// </summary>
        public string? NameOrContent { get; set; }
        /// <summary>
        /// 公告类型：1立即发送2定时发送
        /// </summary>
        public int NoticeType { get; set; } = 1;
        /// <summary>
        /// 定时发送时间
        /// </summary>
        public string? NoticeDate { get; set; }

        //public int MyProperty { get; set; }
    }
}
